﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;
using Model;
using System.Collections;
using UI.Common;
using UI.Lib.Factory;

namespace UI
{
    public class PopService
    {
        private UI.Common.PopItem popitem = null;
        private Hashtable hsRefData = null;

        private string _searchState = "N"; //N|None,S|Success,M|Muti
        private string _where = "";
        private bool _isClear = false; //用户是否输入的“”，用于情况选择框
        private string flag = ""; //如果该参数有值，则证明是下拉框取值
        private DataTable dt = null;

        public string SearchState
        {
            get { return _searchState; }
        }

        public PopService(string _key, Hashtable hs, string _searchWord)
        {
            popitem = PopHelp.GetPopItem(_key, null, hs);

            //模糊查询
            _where = string.Format(" {0} like '{1}' ", popitem.KeyRef, _searchWord);

            Init();
        }

        public PopService(string _key, Hashtable hs, string _searchWord, string flag)
        {
            popitem = PopHelp.GetPopItem(_key, null, hs);

            //模糊查询
            _where = _searchWord;
            this.flag = flag;

            Init();
        }

        private void Init()
        {
            string[] cols = popitem.ShowFields.Split(',');
            if (0 == cols.Length) return;

            StringBuilder scols = new StringBuilder();
            StringBuilder ocols = new StringBuilder();
            string[] sci = null;
            StringBuilder oWhere = new StringBuilder();
            foreach (string s in cols)
            {
                sci = s.Split('|');

                if (0 != scols.Length)
                {
                    scols.Append(",");
                    ocols.Append(",");
                }
                scols.Append(string.Format(" ({0}) AS {1} ", sci[1].Replace("&", ","), sci[0]));
                ocols.Append(sci[0]);
                if (sci.Length < 5 || sci[4].ToLower() == "n") continue;

                if (0 != oWhere.Length) oWhere.Append(" OR ");
                oWhere.Append(string.Format(" {0} LIKE '{1}%' ", sci[0], _where));
            }

            DataRow dr = null;

            if ("A" == flag)
            {
                _where = oWhere.ToString();
            }

            dt = CommonHelper.Entity.GetPopData(popitem.Table, scols.ToString(), ocols.ToString(), popitem.OrderFid, popitem.StrWhere, _where, 20);
            if (null == dt) return;

            if (string.IsNullOrEmpty(flag))
            {
                if (null == dt || dt.Rows.Count == 0) this._searchState = "N";
                else if (dt.Rows.Count > 1) this._searchState = "M";
                else
                {
                    this._searchState = "S";
                    dr = dt.Rows[0];
                }

                hsRefData = new Hashtable();

                hsRefData["$SELF$"] = ("S" == this._searchState) ?
                        dr[popitem.KeyRef] : ""; //当前操作弹出框应该返回何字段

                if (string.IsNullOrEmpty(popitem.RefField)) return;

                string[] refFields = popitem.RefField.Split(',');
                string[] ic = null;
                foreach (string item in refFields)
                {
                    ic = item.Split('|');
                    if (ic.Length < 2) continue;

                    if (ic[1].IndexOf("$VALUE$") > -1) //准备将值写入PopWin的Value属性内
                        hsRefData[ic[1]] = ("S" == this._searchState) ? dr[ic[0]] : "";
                    else
                        hsRefData[ic[1]] = ("S" == this._searchState) ? dr[ic[0]] : "";
                }
            }
        }

        public Hashtable GetSelectedData()
        {
            return hsRefData;
        }

        public DataTable GetListData()
        {
            return dt;
        }

        /// <summary>
        /// 得到下拉建议框显示的字段和值字段
        /// </summary>
        /// <returns></returns>
        public string[] GetListOption()
        {
            string keyMember = "";
            if (!string.IsNullOrEmpty(popitem.RefField))
            {
                string[] refFiends = popitem.RefField.Split(',');
                foreach (string f in refFiends)
                {
                    if (f.IndexOf("$VALUE$") > -1)
                    {
                        keyMember = f.Split('|')[0];
                        break;
                    }
                }
            }

            return new string[] { popitem.KeyRef, string.IsNullOrEmpty(keyMember) ? popitem.KeyRef : keyMember };
        }

        public string GetRefField()
        {
            return popitem.RefField;
        }
    }
}
